const app = getApp()
var MSGINIT = "获取验证码",
  MSGSCUCCESS = "${time}秒后可重发",
  MSGTIME = 60;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: app.globalData.theme, //全局颜色变量
    CustomBar: app.globalData.CustomBar,
    form: {},
    msgText: MSGINIT,
    msgTime: MSGTIME,
    msgKey: false,
    showPrivacyPolicy: true,
    privacyPolicyUrl: "https://zlxc.e-jch.cn/user_privacy_policy.htm",
    protocolUrl: "https://zlxc.e-jch.cn/user_protocol.htm",
    wxLogin: true,
    reUrl: "/pages/home/index",
    userChecked: false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  getPhoneNumber(e) {
    var that = this;
    if (e.detail.encryptedData) {
      app.api.loginByPhoneMa(e.detail).then(function (res) {
        var userInfo = res.data;
        wx.setStorageSync('third_session', userInfo.thirdSession);
        wx.setStorageSync('user_info', userInfo);
        that.loginSuccess();
      });
    }
  },

  handleAgree() {
    wx.showToast({
      title: '请先阅读并同意平台用户协议',
      icon: 'none',
      duration: 3000
    })
  },

  checkboxChange(e) {
    this.setData({
      userChecked: !this.data.userChecked
    })
  },

  //手机号输入
  bindPhoneInput(e) {
    var val = e.detail.value;
    var form = this.data.form;
    form.phone = val;
    this.setData({
      form: form
    });
  },

  //验证码输入
  bindCodeInput(e) {
    var val = e.detail.value;
    var form = this.data.form;
    form.code = val;
    this.setData({
      form: form
    });
  },

  getPhoneCode() {
    var that = this;
    if (this.data.msgKey) return;
    let validateMobile = app.validate.isvalidatemobile(this.data.form.phone)
    if (validateMobile[0]) {
      wx.showToast({
        title: validateMobile[1],
        icon: 'none',
        duration: 3000
      })
      return;
    }
    this.setData({
      msgKey: true
    })
    app.api.getPhoneCode({
      type: '1',
      phone: this.data.form.phone
    }).then(function (res) {
      that.setData({
        msgKey: false
      });
      if (res.code == '0') {
        wx.showToast({
          title: '验证码发送成功',
          icon: 'none',
          duration: 3000
        });

        that.setData({
          msgText: MSGSCUCCESS.replace('${time}', that.data.msgTime),
          msgKey: true
        });
        var time = setInterval(function () {
          that.setData({
            msgTime: that.data.msgTime--,
            msgText: MSGSCUCCESS.replace('${time}', that.data.msgTime)
          });
          if (that.data.msgTime == 0) {
            that.setData({
              msgTime: MSGTIME,
              msgText: MSGINIT,
              msgKey: false
            });
            clearInterval(time);
          }
        }, 1000);
      } else {

      }
    }).catch(function () {
      that.setData({
        msgKey: false
      });
    });
  },
  loginSub(e) {
    var that = this;
    let validateMobile = app.validate.isvalidatemobile(this.data.form.phone)
    if (validateMobile[0]) {
      wx.showToast({
        title: validateMobile[1],
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (!this.data.form.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 3000
      });

      return;
    }
    app.api.loginByPhone(this.data.form).then(function (res) {
      var userInfo = res.data;
      wx.setStorageSync('third_session', userInfo.thirdSession);
      wx.setStorageSync('user_info', userInfo);
      that.loginSuccess();
    });
  },
  loginSuccess() {
    var that = this;
    // 登录完成跳到首页
    wx.reLaunch({
      url: decodeURIComponent(that.data.reUrl)
    });
    // 获取购物车数量
    // this.shoppingCartCount();
  },
  loginChange() {
    this.setData({
      wxLogin: !this.data.wxLogin
    })
  }
})